package com.example.fp.cgc.controller

import com.example.fp.cgc.connerlt.AlarmTask
import com.example.fp.cgc.mapper.UserSettingMapper
import com.example.fp.cgc.util.DataUtil
import com.example.fp.cgc.websocket.WebSocketServer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import springfox.documentation.swagger2.annotations.EnableSwagger2

/**
 * @since 2020-02-28
 * @author 蔡
 */
@RestController
@RequestMapping
@EnableSwagger2
open class UserSettingController {
    @Autowired
    @Lazy
    lateinit var userSettingMapper: UserSettingMapper

    /**
     * 更改关注状态
     * @since 2020-02-28
     * @author 蔡
     */
    @GetMapping(value = ["/changeSetting/{uid}&{type}"])
    open fun changeSetting(
            @PathVariable("uid") uid: Int,
            @PathVariable("type") type: String
    ) {
        val state = userSettingMapper.getState(uid)
        val userSetting = state[0]
        when (type) {
            "offline" -> {
                userSettingMapper.setOffline(uid, !userSetting.offline)
            }
            "history" -> {
                userSettingMapper.setHistory(uid, !userSetting.history)
                if (!userSetting.history) {
                    WebSocketServer.onlineUserMap[uid]?.sendMessage(DataUtil.getSettingState(uid))
                    WebSocketServer.onlineUserMap[uid]?.sendMessage(DataUtil.getUserHistoryList(uid))
                }
            }
            "unread" -> {
                userSettingMapper.setUnread(uid, !userSetting.unread)
                WebSocketServer.onlineUserMap[uid]?.sendMessage(DataUtil.getSettingState(uid))
                if (!userSetting.unread) {
                    WebSocketServer.onlineUserMap[uid]?.sendMessage(AlarmTask.task.getMessageState(uid))
                }
            }
        }
    }
}